<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
class Search extends MX_Controller
{

    private $_langCode;

    function __construct()
    {
        parent::__construct();
        $this->load->library('Layout');
        $this->_langCode = $this->mycommon->getLangCode();
        $this->load->model('post/post_model');
    }

    public function index($page = '')
    {
        $data = array();
        $keyword = $this->input->get('tukhoa', true);
        if ($keyword == '') {
            die('Not Found!~');
            show_404();
            exit;
        }
        $page = (intval($page) != 0) ? intval($page) : 1;
        $langCode = $this->mycommon->fetchLangCode($this->_langCode);
        $this->layout->setMenuActive(0);
        $data['headTitle'] = 'Tìm kiếm';
        $data['catInfo'] = 'Tìm kiếm';
        $this->load->library('Pagination');
        $numPost = $this->post_model->getNumPostLikeKeyword($keyword, $langCode);
        $config['base_url'] = base_url('tim-kiem');
        $config['prefix'] = ($this->_langCode == 'vietnam') ? 'trang_' : 'page_';
        $config['suffix'] = '.html?tukhoa='.$keyword;
        $config['total_rows'] = $numPost;
        $config['per_page'] = 5;
        $config['cur_page'] = $page;
        $config['use_page_numbers'] = TRUE;
        $config['first_link'] = '«';
        $config['last_link'] = '»';
        $config['cur_tag_open'] = '<a class="active-page">';
        $config['cur_tag_close'] = '</a>';
        $config['num_links'] = 6;
        $config['uri_segment'] = 3;
        $config['first_url'] = site_url('tim-kiem') . '?tukhoa='.$keyword;
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $tmpPost = $this->post_model->getPostLikeKeyword($keyword, $config['per_page'], $start, $langCode);
        $data['post'] = $tmpPost;
        $data['total'] = $numPost;
        $data['keyword'] = $keyword;
        $this->layout->view('search/search_view', $data);
    }

}

?>
